371A - K-Periodic Array - CodeForces Solution


greedy implementation math *1000

Please click on ads to support us..

Python Code:

n, k = map(int, input().split())
a = list(map(int, input().split()))

count = 0
if(len(a)%k==0):    
    for i in range(k):
        b = a[i::k]
        count += min(b.count(1),b.count(2))
print(count)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int N=1e7+10;
#define ll long long int
#define pb push_back
//#define getUnique(v) {sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());}
ll gcd(ll a,ll b){if(a%b==0)return b;return gcd(b,a%b);}
ll binexp(ll n,ll p){ll res = 1;while (p>0){if(p&1)res=res*n;n=n*n;p>>=1;}return res;}
void solve(){
    ll n,k;cin>>n>>k;
    ll a[n];
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    ll ans=0;//k<=n/2
    for(int i=1;i<=k;i++){
        int one=0,two=0;
        for(int j=i;j<=n;j+=k){
            if(a[j]==1)one++;
            else two++;
        }
        ans+=min(one,two);
    }
    cout<<ans;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("error.txt", "w", stderr);
freopen("output.txt", "w", stdout);
#endif
    int t;t=1;
    while(t--)
        solve();
}


Comments

Submit
0 Comments
More Questions

842B - Gleb And Pizza
1746D - Paths on the Tree
1651E - Sum of Matchings
19A - World Football Cup
630P - Area of a Star
1030C - Vasya and Golden Ticket
1529D - Kavi on Pairing Duty
1743A - Password
1743B - Permutation Value
1743C - Save the Magazines
1743D - Problem with Random Tests
1070K - Video Posts
767C - Garland
1201B - Zero Array
1584C - Two Arrays
1131C - Birthday
1285B - Just Eat It
1743F - Intersection and Union
771A - Bear and Friendship Condition
1208E - Let Them Slide
656A - Da Vinci Powers
1025A - Doggo Recoloring
257A - Sockets
231C - To Add or Not to Add
1454E - Number of Simple Paths
931B - World Cup
934B - A Prosperous Lot
999B - Reversing Encryption
1238D - AB-string
810B - Summer sell-off